package com.playball.lottery.customview;

import com.playball.lottery.R;
import com.playball.lottery.customview.MyDialog.Builder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 自定义 对话框
* @ClassName: ShowCustomDialog 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author vanszhen 
* @date 2014年6月24日 下午3:32:12 
*
 */
public class ShowCustomDialog  extends  Dialog{

	public ShowCustomDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	/**主题的构造*/
    public ShowCustomDialog(Context context, int theme) {
        super(context, theme);
    }
	
	  /**内部类*/
	  public static class Builder{
		  
		    /**上下文*/
		    private Context context;
		    /**标题*/
	        private String title;
	        /**信息*/
	        private String message;
	        /**左面按钮文字*/
	        private String positiveButtonText;
	        /**又面按钮文字*/
	        private String negativeButtonText;
	        /**中间面按钮文字*/
	        private String  neutralButtonText;
	        /**View*/
	        private View contentView;
	        /** 提示图片 */
	        private int imgId = -1;
	        
	        public Builder(Context context) {
	            this.context = context;
	        }
	        
	        private DialogInterface.OnClickListener 
            positiveButtonClickListener,
            neutralButtonClickListener,
            negativeButtonClickListener
          
            ; 
	        //imgDeleteButtonClickListener
	        private DialogInterface.OnCancelListener
			onCancelListener;
	      
	        /**
	         * Set the Dialog message from String
	         * @param title
	         * @return
	         */
	        public Builder setMessage(String message) {
	            this.message = message;
	            return this;
	        }
	 
	        /**
	         * Set the Dialog message from resource
	         * @param title
	         * @return
	         */
	        public Builder setMessage(int message) {
	            this.message = (String) context.getText(message);
	            return this;
	        }
	 
	        /**
	         * Set the Dialog title from resource
	         * @param title
	         * @return
	         */
	        public Builder setTitle(int title) {
	            this.title = (String) context.getText(title);
	            return this;
	        }
	 
	        /**
	         * Set the Dialog title from String
	         * @param title
	         * @return
	         */
	        public Builder setTitle(String title) {
	            this.title = title;
	            return this;
	        }
	 
	        /**
	         * Set a custom content view for the Dialog.
	         * If a message is set, the contentView is not
	         * added to the Dialog...
	         * @param v
	         * @return
	         */
	        public Builder setContentView(View v) {
	            this.contentView = v;
	            return this;
	        }
	 
	        /**
	         * Set the positive button resource and it's listener
	         * @param positiveButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setPositiveButton(int positiveButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.positiveButtonText = (String) context
	                    .getText(positiveButtonText);
	            this.positiveButtonClickListener = listener;
	            return this;
	        }
	 
	        /**
	         * Set the positive button text and it's listener
	         * @param positiveButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setPositiveButton(String positiveButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.positiveButtonText = positiveButtonText;
	            this.positiveButtonClickListener = listener;
	            return this;
	        }
	 
	        /**
	         * Set the negative button resource and it's listener
	         * @param negativeButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setNegativeButton(int negativeButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.negativeButtonText = (String) context
	                    .getText(negativeButtonText);
	            this.negativeButtonClickListener = listener;
	            return this;
	        }
	 
	        /**
	         * Set the negative button text and it's listener
	         * @param negativeButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setNegativeButton(String negativeButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.negativeButtonText = negativeButtonText;
	            this.negativeButtonClickListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set the Neutral button resource and it's listener
	         * @param negativeButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setNeutralButton(int neutralButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.neutralButtonText = (String) context
	                    .getText(neutralButtonText);
	            this.negativeButtonClickListener = listener;
	            return this;
	        }
	 
	        /**
	         * Set the Neutral button text and it's listener
	         * @param negativeButtonText
	         * @param listener
	         * @return
	         */
	        public Builder setNeutralButton(String neutralButtonText,
	                DialogInterface.OnClickListener listener) {
	            this.neutralButtonText = neutralButtonText;
	            this.negativeButtonClickListener = listener;
	            return this;
	        }
	        
	        /**
	         * Set the imgDele button it's listener
	         * @param listener
	         * @return
	         */
	    /*    public Builder setImgDelButton( DialogInterface.OnClickListener listener){
	        	  this.imgDeleteButtonClickListener = listener;
		          return this;
	        }*/
	        
	        public Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
	        	this.onCancelListener = onCancelListener;
	        	return this;
	        }
	        
	        /**
	         * 自定义提示图片
	         */
	        public Builder setMessageIcon(int resId) {
	        	this.imgId = resId;
	        	return this;
	        }
	        
		  
		  /**自定义*/
	        public ShowCustomDialog createCustomDialog() {
	        	
	            LayoutInflater inflater = (LayoutInflater) context
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            final ShowCustomDialog dialog = new ShowCustomDialog(context, R.style.MyDialog);
                dialog.setCanceledOnTouchOutside(false);	
                dialog.setCancelable(true);
                View layout = inflater.inflate(R.layout.jdd_coustom_dialog_layout, null);
                
                
    			Window dialogWindow=dialog.getWindow();
    			DisplayMetrics metrics=new DisplayMetrics();
    			((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    			int width=(int) ((metrics.widthPixels)*0.5);
    			WindowManager.LayoutParams layoutParams=dialog.getWindow().getAttributes();
    			layoutParams.width=width;
    			dialogWindow.setAttributes(layoutParams);
                
        		dialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
        				LinearLayout.LayoutParams.WRAP_CONTENT));
                
                //自定义 title
                ((TextView) layout.findViewById(R.id.title_custom)).setText(title);
                
                
              //ji
                if(onCancelListener != null) {
                	dialog.setOnCancelListener(onCancelListener);
                }
                
                //左面按钮
                if (positiveButtonText != null) {
                	 ((Button) layout.findViewById(R.id.positiveButton_btn_custom))
                     .setText(positiveButtonText );
                	 
                	//左面按钮触发事件
                 	if (positiveButtonClickListener != null) {
                 		 ((Button) layout.findViewById(R.id.positiveButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
 							@Override
 							public void onClick(View v) {
 								positiveButtonClickListener.onClick(dialog, 
 										DialogInterface.BUTTON_POSITIVE
 										);
 								
 							}
 						});
                 		
 					}else {
 					      // if no confirm button just set the visibility to GONE
 		                layout.findViewById(R.id.positiveButton_btn_custom).setVisibility(
 		                        View.GONE);
 					}
                } else {
                	 layout.findViewById(R.id.positiveButton_btn_custom).setVisibility(
		                        View.GONE);
				}
                
                
                //中间面按钮
                if (neutralButtonText != null) {
                    ((Button) layout.findViewById(R.id.neutralButton_btn_custom))
                    .setText(neutralButtonText);
                    
                    if (neutralButtonClickListener != null) {
						
                        ((Button) layout.findViewById(R.id.neutralButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								neutralButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
							}
						});	
                    	
					}else {
						  layout.findViewById(R.id.neutralButton_btn_custom).setVisibility(
			                        View.GONE);
					}
                    
                	
				}else {
					layout.findViewById(R.id.neutralButton_btn_custom).setVisibility(
	                        View.GONE);
				}
                
                //右面按钮
                if (negativeButtonText != null) {
                    ((Button) layout.findViewById(R.id.negativeButton_btn_custom))
                    .setText(negativeButtonText);
                    
                    if (negativeButtonClickListener != null) {
						
                        ((Button) layout.findViewById(R.id.negativeButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
							}
						});	
                    	
					}else {
						  layout.findViewById(R.id.negativeButton_btn_custom).setVisibility(
			                        View.GONE);
					}
                    
                	
				}else {
					 layout.findViewById(R.id.negativeButton_btn_custom).setVisibility(
		                        View.GONE);
				}

                
                
                //取消按钮
               // if (imgDeleteButtonClickListener != null) {
                ((ImageView) layout.findViewById(R.id.img_dele_custom)).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.cancel();
						//imgDeleteButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
					}
                 });
               /* }else {
                	  layout.findViewById(R.id.img_dele_custom).setVisibility(
		                        View.GONE);
				}*/
                
                //信息
                if (message != null) {
                    ((TextView) layout.findViewById(
                    		R.id.message_custom)).setText(message);	
				}else if (contentView != null) {
					
		              ((LinearLayout) layout.findViewById(R.id.content_custom))
                      .removeAllViews();
                     ((LinearLayout) layout.findViewById(R.id.content_custom))
                      .addView(contentView, 
                              new LayoutParams(
                                      LayoutParams.WRAP_CONTENT, 
                                      LayoutParams.WRAP_CONTENT));
					
				}
                
                ImageView iv = (ImageView) layout.findViewById(R.id.image_custom);
                if(imgId > 0) {
                	iv.setBackgroundResource(imgId);
                	iv.setVisibility(View.VISIBLE);
                } else {
                	iv.setVisibility(View.GONE);
                }
                
              dialog.setContentView(layout);  
	        	
				return dialog;
	        	
			}
		  
	  }
	
	
	
	
	
	
	

}
